package com.miss.spjyx.network

import android.content.Context
import com.ihsanbal.logging.Level
import com.ihsanbal.logging.LoggingInterceptor
import com.miss.spjyx.BuildConfig
import com.miss.spjyx.CkApp
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 * 请求类
 *
 * @author wl
 * @since 2019/08/31 12:47
 */
class Requester {
    companion object {
        private fun <T> getService(baseUrl: String, service: Class<T>): T {
            val logInterceptor = LoggingInterceptor.Builder()
                .loggable(BuildConfig.DEBUG)
                .setLevel(Level.BASIC)
                .tag("WL_request")
                .build()

            val client = OkHttpClient.Builder()
                .addInterceptor(logInterceptor)
                .build()

            val retrofit = Retrofit.Builder().baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build()
            return retrofit.create(service)
        }

        fun apiService() :ApiService {
            val sp = CkApp.app.getSharedPreferences("spj", Context.MODE_PRIVATE)
            val url = sp.getString("set_ckdz", "")
            return getService(url ?: "", ApiService::class.java)
        }
    }
}